import { _decorator, Component, director, Node, Event, SpriteFrame, Sprite, Enum, resources, SpriteAtlas, find } from "cc";
import SoundManager from "../Manager/SoundManager";
import { GameData } from "../Data/GameData";
import { LevelItem } from "../LevelItem";
import Banner from "../Tools/Banner";
import { Login } from "../../_RealName/Login";

const { ccclass, property } = _decorator;


enum AgeLimit {
    八岁,
    十二岁,
    十六岁
}

@ccclass
export default class MainUI extends Component {
    private static instance: MainUI;
    static get Instance() {
        return this.instance;
    }

    @property({ type: Enum(AgeLimit), displayName: `适龄`, tooltip: `适龄限制` })
    ageLimit: AgeLimit = AgeLimit.八岁;

    @property(Node)
    StartButton: Node;
    @property(Node)
    Levels: Node;
    @property(Node)
    LevelSelectionPanel: Node;
    @property(SpriteFrame)
    defaultSf: SpriteFrame;
    @property([SpriteFrame])
    levelSfs: SpriteFrame[] = [];

    ageLimitAtalsPath: string = `Sprites/Common/AgeLimit`;

    onLoad() {
        MainUI.instance = this;
    }

    start() {
        this.loadAgeLimitSp();
    }

    loadAgeLimitSp() {
        //适龄提示角标
        let ageLimitSp: Sprite = find("bg/AgeLimitSp", this.node).getComponent(Sprite);
        switch (this.ageLimit) {
            case AgeLimit.八岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`8`);
                });
                break;
            case AgeLimit.十二岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`12`);
                });
                break;
            case AgeLimit.十六岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`16`);
                });
                break;
        }
    }

    Back() {
        this.StartButton.active = true;
        this.LevelSelectionPanel.active = false;
        SoundManager.Instance.PlayButton();
    }

    LevelShow() {
        this.LevelSelectionPanel.active = true;
        SoundManager.Instance.PlayButton();
        for (let i = 0; i < this.Levels.children.length; i++) {
            var levelItem = this.Levels.children[i];
            if (i < GameData.Unlock) levelItem.getComponent(LevelItem).unlock();
            const index = GameData.Cleared.findIndex(e => e == i + 1);
            // const sf = this.levelSfs[i];
            const sf = index != -1 ? this.levelSfs[i] : this.defaultSf;
            levelItem.getComponent(LevelItem).init(i + 1, sf);
        }
    }

    Play(event: Event) {
        const target = event.target;
        const levelItem: LevelItem = target.getComponent(LevelItem);
        SoundManager.Instance.PlayButton();
        if (!levelItem.IsUnlock) return;
        GameData.levelNum = levelItem.LevelNum;
        GameData.scene = "GameScene";
        director.loadScene("LoadScene");
    }

    ageClick() {
        Login.Instance.showRightAgePanel();
    }

    PrivacyButton() {
        Banner.Instance.AndroidPrivacy();
    }
}
